import 'dart:async';
import 'dart:convert' as json;

import 'package:dio/dio.dart';
import 'package:icooker/converts/json/base_bean_entry.dart';
import 'package:icooker/repository/base_model.dart';
import 'package:kasax/kasax.dart';

/// http服务提供
class HttpService {
  static HttpService _instance = HttpService._internal();

  factory HttpService() => _instance;

  HttpService._internal();

  static HttpService getInstance() => _instance;

  /// get请求
  Future<dynamic> get(path, {params}) async {
    var reslut;
    if (params != null) {
      reslut = await DioManager.getInstance()
          .get(path, params: params, autoConvert: false);
    } else {
      reslut = await DioManager.getInstance().get(path, autoConvert: false);
    }
    if (reslut != null) {
      if (reslut is Map) {
        return BaseModel.fromJson(reslut);
      } else {
        return BaseModel.fromJson(json.jsonDecode(reslut));
      }
    } else {
      var compiler = Completer();
      var f = compiler.future;
      compiler.complete(true);
      return f;
    }
  }

  /// get请求
  Future<T?> getWithEntry<T>(path, {params}) async {
    var reslut;
    if (params != null) {
      reslut = await DioManager.getInstance()
          .get(path, params: params, autoConvert: false);
    } else {
      reslut = await DioManager.getInstance().get(path, autoConvert: false);
    }
    T? entry;
    if (reslut != null) {
      try {
        var baseBeanEntry = BaseBeanEntry.fromJson(json.jsonDecode(reslut));
        if (baseBeanEntry != null) {
          entry = baseBeanEntry.getObj<T>();
        }
      } catch (e) {
        print(e);
      }
    }
    return entry;
  }

  /// get请求
  Future<List<T?>?> getWithEntrys<T>(path, {params}) async {
    var reslut;
    if (params != null) {
      reslut = await DioManager.getInstance()
          .get(path, params: params, autoConvert: false);
    } else {
      reslut = await DioManager.getInstance().get(path, autoConvert: false);
    }
    List<T?>? list = [];
    if (reslut != null) {
      try {
        var baseBeanEntry = BaseBeanEntry.fromJson(json.jsonDecode(reslut));
        if (baseBeanEntry != null) {
          list = baseBeanEntry.getList<T>();
        }
      } catch (e) {
        print(e);
      }
    }
    return list;
  }

  /// post请求
  Future<dynamic> post(path, {Map<String, dynamic>? data}) async {
    Response response;
    if (data != null) {
      response = await DioManager.getInstance()
          .post(path, data: data, autoConvert: false);
    } else {
      response = await DioManager.getInstance().post(path, autoConvert: false);
    }
    if (response != null) {
      if (response.statusCode == 200) {
        return BaseModel.fromJson(response.data);
      }
    } else {
      var compiler = Completer();
      var f = compiler.future;
      compiler.complete(true);
      return f;
    }
  }

  /// post请求
  Future<T?> postWithEntry<T>(path, {params}) async {
    var reslut;
    if (params != null) {
      reslut = await DioManager.getInstance()
          .post(path, queryParameters: params, autoConvert: false);
    } else {
      reslut = await DioManager.getInstance().post(path, autoConvert: false);
    }
    T? entry;
    if (reslut != null) {
      try {
        var baseBeanEntry = BaseBeanEntry.fromJson(json.jsonDecode(reslut));
        if (baseBeanEntry != null) {
          entry = baseBeanEntry.getObj<T>();
        }
      } catch (e) {
        print(e);
      }
    }
    return entry;
  }

  /// post请求
  Future<List<T?>?> postWithEntrys<T>(path, {params}) async {
    var reslut;
    if (params != null) {
      // diomanager不支持，需要调整
      reslut = await DioManager.getInstance()
          .post(path, queryParameters: params, autoConvert: false);
    } else {
      reslut = await DioManager.getInstance().post(path, autoConvert: false);
    }
    List<T?>? list = [];
    if (reslut != null) {
      try {
        var baseBeanEntry = BaseBeanEntry.fromJson(json.jsonDecode(reslut));
        if (baseBeanEntry != null) {
          list = baseBeanEntry.getList<T>();
        }
      } catch (e) {
        print(e);
      }
    }
    return list;
  }
}
